package com.danskin.wechat.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.danskin.wechat.dao.RelFansTagDao;
import com.danskin.wechat.dao.WxTagInfoDao;
import com.danskin.wechat.entity.RelFansTag;
import com.danskin.wechat.entity.WxTagInfo;
import com.danskin.wechat.entity.example.RelFansTagExample;
import com.danskin.wechat.entity.example.WxTagInfoExample;
import com.danskin.wechat.entity.example.WxTagInfoExample.Criteria;
import com.danskin.wechat.service.WxTagInfoService;
import com.danskin.wechat.util.HttpClientUtil;
import com.danskin.wechat.util.JsonUtils;
import com.danskin.wechat.util.Page;
@Service
public class WxTagInfoServiceImpl implements WxTagInfoService {

	@Autowired
	private WxTagInfoDao wxTagInfoDao;
	@Autowired
	private RelFansTagDao relFansTagDao;
	
	@Value("${createTagUrl}")
	public String createTagUrl;
	@Value("${updateTagUrl}")
	public String updateTagUrl;
	@Value("${deleteTagUrl}")
	public String deleteTagUrl;
	
	
	@Override
	public WxTagInfo getWxTagInfoByByPrimaryKey(Integer id) {
		WxTagInfo wxTagInfo = wxTagInfoDao.selectByPrimaryKey(id);
		return wxTagInfo;
	}

	@Override
	public List<WxTagInfo> getAllWxTagInfo() {
		WxTagInfoExample example=new WxTagInfoExample();
		Criteria createCriteria = example.createCriteria();
		createCriteria.andIdIsNotNull();
		List<WxTagInfo> list = wxTagInfoDao.selectByExample(example);
		return list;
	}
	@Override
	public List<WxTagInfo> getUsedFansTag(Map<String,Object> map) {
		List<WxTagInfo> list = wxTagInfoDao.selectUsedWxTagInfoByFansId(map);
		return list;
	}

	@Override
	public int insertRelFansTagBatch(List<RelFansTag> fansTagList) {
		int i=0;
		RelFansTagExample example=new RelFansTagExample();
		com.danskin.wechat.entity.example.RelFansTagExample.Criteria criteria = example.createCriteria();
		for (RelFansTag relFansTag : fansTagList) {
			i+=relFansTagDao.insertSelective(relFansTag);
		}
		
		return i;
	}
	
	@Override
	public int deleteRelFansTagBatch(List<RelFansTag> fansTagList) {
		int i=0;
		RelFansTagExample example=new RelFansTagExample();
		com.danskin.wechat.entity.example.RelFansTagExample.Criteria criteria = example.createCriteria();
		for (RelFansTag relFansTag : fansTagList) {
			criteria.andMpIdEqualTo(relFansTag.getMpId());
			criteria.andTagIdEqualTo(relFansTag.getTagId());
			criteria.andFansIdEqualTo(relFansTag.getFansId());
			i+=relFansTagDao.deleteByExample(example);
		}
		return i;
	}
	

	@Override
	public List<WxTagInfo> listPageWxTagInfo(Page page) {
		List<WxTagInfo> list = wxTagInfoDao.listPageWxTagInfo(page);
		return list;
	}

	@Override
	public int updateWxTagInfo(Integer id, String tagName, String token,Integer tagId) {
		JSONObject map=new JSONObject();
		JSONObject map2=new JSONObject();
		int i=0;
		map.put("id", tagId);
		map.put("name", tagName);
		map2.put("tag", map);
		String string = HttpClientUtil.doPostJson(updateTagUrl+token, map2.toString());
		JSONObject JSON = JsonUtils.jsonToPojo(string, JSONObject.class);
		if(JSON.getInteger("errcode")==0){
			System.out.println(string);
			WxTagInfo tagInfo=new WxTagInfo();
			tagInfo.setId(id);
			tagInfo.setTagName(tagName);
			i = wxTagInfoDao.updateByPrimaryKeySelective(tagInfo);
		}
		return i;
	}

	@Override
	public int saveWxTagInfo(String tagName, String token) {
		JSONObject map=new JSONObject();
		JSONObject map2=new JSONObject();
		int i=0;
		map.put("name", tagName);
		map2.put("tag", map);
		String string = HttpClientUtil.doPostJson(createTagUrl+token, map2.toString());
		JSONObject JSON = JsonUtils.jsonToPojo(string, JSONObject.class);
		JSONObject object = JSON.getJSONObject("tag");
		if(!JSON.containsKey("errcode")){
			String name = object.getString("name");
			Integer id = object.getInteger("id");
			WxTagInfo tagInfo=new WxTagInfo();
			tagInfo.setWxTagId(id);
			tagInfo.setTagName(name);
			tagInfo.setCreatedTime(new Date());
			i = wxTagInfoDao.insertSelective(tagInfo);
		}
		return i;
	}

	@Override
	public int deleteWxTagInfo(Integer id, Integer tagId, String token) {
		int i=0;
		JSONObject map=new JSONObject();
		JSONObject map2=new JSONObject();
		map.put("id", tagId);
		map2.put("tag", map);
		String string = HttpClientUtil.doPostJson(deleteTagUrl+token, map2.toString());
		JSONObject JSON = JsonUtils.jsonToPojo(string, JSONObject.class);
		if(JSON.getInteger("errcode")==0){
			System.out.println(string);
			WxTagInfo tagInfo=new WxTagInfo();
			tagInfo.setId(id);
			i = wxTagInfoDao.deleteByPrimaryKey(id);
		}
		return i;
	}

	@Override
	public List<WxTagInfo> selectWxTagInfoByFansIdExclude(Map<String, Object> map) {
		List<WxTagInfo> list = wxTagInfoDao.selectWxTagInfoByFansIdExclude(map);
		return list;
	}

	
	


}
